﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfApplication2.SindicatoWebService;
using WpfApplication2.Helpers;
using System.Windows.Input;
using System.Windows;
using System.Threading;
using System.Windows.Navigation;

namespace WpfApplication2.ViewModels.Usuario
{
    public class ActivarUsuarioViewModel : ViewModel
    {
        #region properties

        public SindicatoWebService.Service1SoapClient WS;
        NavigationService NS
        {
            get;
            set;
        }

        public UsuarioDTO Usuario
        {
            get;
            set;
        }

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                if (password != value)
                {
                    password = value;
                    RaisePropertyChanged("Password");
                }
            }
        }
        private string password;

        public string Titulo
        {
            get
            {
                return titulo;
            }
            set
            {
                if (titulo != value)
                {
                    titulo = value;
                    RaisePropertyChanged("Titulo");
                }
            }
        }
        private string titulo;

        public string ErrorMessage
        {
            get
            {
                return errorMessage;
            }
            set
            {
                if (errorMessage != value)
                {
                    errorMessage = value;
                    RaisePropertyChanged("ErrorMessage");
                }
            }
        }
        private string errorMessage;

        public string PasswordConfirmation
        {
            get
            {
                return passwordConfirmation;
            }
            set
            {
                if (passwordConfirmation != value)
                {
                    passwordConfirmation = value;
                    RaisePropertyChanged("PasswordConfirmation");
                }
            }
        }
        private string passwordConfirmation;

        public Visibility ErrorVisibility
        {
            get
            {
                return errorVisibility;
            }
            set
            {
                if (errorVisibility != value)
                {
                    errorVisibility = value;
                    RaisePropertyChanged("ErrorVisibility");
                }
            }
        }
        private Visibility errorVisibility;


        public Visibility ExitoVisibility
        {
            get
            {
                return exitoVisibility;
            }
            set
            {
                if (exitoVisibility != value)
                {
                    exitoVisibility = value;
                    RaisePropertyChanged("ExitoVisibility");
                }
            }
        }
        private Visibility exitoVisibility;


        public Visibility FormVisibility
        {
            get
            {
                return formVisibility;
            }
            set
            {
                if (formVisibility != value)
                {
                    formVisibility = value;
                    RaisePropertyChanged("FormVisibility");
                }
            }
        }
        private Visibility formVisibility;

        #endregion

        public ActivarUsuarioViewModel(UsuarioDTO usuario, NavigationService ns)
        {
            WS = new Service1SoapClient();
            NS = ns;
            Usuario = usuario;
            Titulo = String.Format("Bienvenido {0}", usuario.UserName);
            FormVisibility = Visibility.Visible;
            ExitoVisibility = Visibility.Collapsed;
            ErrorVisibility = Visibility.Collapsed;
        }


        private DelegateCommand activarUsuario;

        public ICommand ActivarUsuario
        {
            get
            {
                if (activarUsuario == null)
                {
                    activarUsuario = new DelegateCommand(Activar, CanExecute);
                }
                return activarUsuario;
            }
        }

        private bool CanExecute()
        {
            return true;
        }

        private void Activar()
        {
            if (string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(PasswordConfirmation))
            {
                ErrorVisibility = Visibility.Visible;
                ErrorMessage = "Debe completar los dos campos con su nueva contraseña";
            }
            else
            {
                if (PasswordConfirmation != Password)
                {
                    ErrorVisibility = Visibility.Visible;
                    ErrorMessage = "Las contraseñas no coinciden. Intente nuevamente.";
                }
                else
                {
                    if (!SecurityHelper.ValidatePassword(Password))
                    {
                        ErrorVisibility = Visibility.Visible;
                        ErrorMessage = "La contraseña no cumple con el formato requerido. Por favor introduzca una nueva contraseña.";
                    }
                    else
                    {
                        WS.ActivarUsuario(Usuario.Id, SecurityHelper.Encriptar(Password));
                        ExitoVisibility = Visibility.Visible;
                        ErrorVisibility = Visibility.Collapsed;
                        FormVisibility = Visibility.Collapsed;
                    }
                }
            }
        }
    }
}
